require 'ipaddr'
module ProxyPump
  module Helpers

    extend self

    # Determine whether the specified object is an IO.
    #
    def io?(obj)
      obj.respond_to?(:read) &&
      obj.respond_to?(:eof?) &&
      obj.respond_to?(:rewind)
    end

    def valid_ip?(ip)
      begin
        ip = IPAddr.new ip.to_s
        ip.ipv4? || ip.ipv6?
      rescue => e
        false
      end
    end

    def valid_port?(port)
      port = port.to_i
      port > 1 && port < 65535
    end

    def sqlite_ftime(time)
      DateTime.parse(time.to_s).strftime '%Y-%m-%d %H:%M:%S'
    end

  end # Helpers
end # ProxyPump
